/**
 * @Author Guy Franco
 *
 * 	this is the Execution Leader Pairing Panel able to search a  request and an execution leader, 
 * and pair them
 */


package gui;

import javax.swing.JPanel;

import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class ExecutionLeaderPairingPanel extends JPanel implements ChatIF{
	private AcademicFrame frame;
	private ExecutionLeaderPairingPanel executionLeaderPairingPanel;
	private SupervisorPanel supervisorPanel;
	private JComboBox<String> rcomboBox;
	private JComboBox <String> comboBox;
	private ViewRequestPanel viewRequest;
	public ExecutionLeaderPairingPanel (AcademicFrame frame,SupervisorPanel supervisorPanel)
	{
		super();
		this.frame=frame;
		this.supervisorPanel=supervisorPanel;
		this.executionLeaderPairingPanel=this;
		frame.setSize(500, 400);
		initialize();
		
	}
	
	
private void initialize() {
		setLayout(null);
		
		JLabel lblExecutionLeaderPairing = new JLabel("Execution Leader Pairing");
		lblExecutionLeaderPairing.setForeground(Color.BLUE);
		lblExecutionLeaderPairing.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblExecutionLeaderPairing.setBounds(96, 25, 232, 22);
		add(lblExecutionLeaderPairing);
		
		JLabel lblChooseARequest = new JLabel("Choose a Request");
		lblChooseARequest.setBounds(31, 80, 114, 14);
		add(lblChooseARequest);
		
		JLabel lblChooseAnExecution = new JLabel("Choose an Execution Leader");
		lblChooseAnExecution.setBounds(211, 80, 155, 14);
		add(lblChooseAnExecution);
		
		rcomboBox = new JComboBox();
		rcomboBox.setBounds(31, 105, 127, 20);
		add(rcomboBox);
		
		comboBox = new JComboBox();
		comboBox.setBounds(229, 105, 137, 20);
		add(comboBox);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))// filter invalid choice
				{
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				}
				else{
					
				viewRequest = new ViewRequestPanel(rcomboBox.getSelectedItem().toString(),frame,executionLeaderPairingPanel,false,"");
				
				frame.setSize(550, 730);
				
				frame.setContentPane(viewRequest);
				}
			}
		});
		btnDetails.setBounds(47, 147, 89, 23);
		add(btnDetails);
		
		JButton btnDetails_1 = new JButton("Details");
		btnDetails_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(comboBox.getSelectedItem().equals("--Choose an Execution Leader--"))
				{
					(new Dialog("Please choose an Assessor from the list first.")).setVisible(true);
				}
				else{
					ArrayList<Object> request =new ArrayList<Object>();
					request.add("getUserName");
					request.add(comboBox.getSelectedItem());
					AcademicFrame.myclient.sendMessageToServer(request);
				}
			}
		});
		btnDetails_1.setBounds(247, 147, 89, 23);
		add(btnDetails_1);
		
		JButton btnPair = new JButton("Pair");
		btnPair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rcomboBox.getSelectedItem().equals("--Choose a Request--"))
				{
				(new Dialog("Illigal Items to Pair")).setVisible(true);
				}
				else
					if(comboBox.getSelectedItem().equals("--Choose an Execution Leader--"))
					{
						(new Dialog("Illigal Items to Pair")).setVisible(true);
					}
					else
					
						if((rcomboBox.getSelectedItem().equals("--Choose a Request--"))&&(comboBox.getSelectedItem().equals("--Choose an Execution Leader--")))
						{
							(new Dialog("Illigal Items to Pair")).setVisible(true);
						}
							else
							{
								ArrayList<Object> pair =new ArrayList<Object>();
								pair.add("Pair");
								pair.add(rcomboBox.getSelectedItem());
								pair.add(comboBox.getSelectedItem());
								pair.add("Elid");
								pair.add("8");
								AcademicFrame.myclient.sendMessageToServer(pair);
							}
			
			}
		});
		btnPair.setBounds(247, 210, 89, 23);
		add(btnPair);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);
			}
		});
		btnCancel.setBounds(47, 210, 89, 23);
		add(btnCancel);
	}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if(answer.get(0).equals("ridlist"))
		{
			if(answer.get(2).equals("0"))
			{
				(new Dialog("No Requests To Pair To")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(supervisorPanel);	
			}
			else
			{
			int numberOfSystems = Integer.parseInt((answer.get(1)).toString()); // get the number of systems in the list
			this.rcomboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.rcomboBox.addItem((String)answer.get(i+2));  // add systems to the list
			}
			ArrayList<Object> exeleader= new ArrayList<Object>();
			exeleader.add("getExe");	
			AcademicFrame.myclient.sendMessageToServer(exeleader); // get assessor
			}
			
		}
		else
			if(answer.get(0).equals("executionLeader"))
			{
				if(answer.get(2).equals("EmptyList1"))
				{
					(new Dialog("No Available Execution Leader")).setVisible(true);
					frame.setSize(500, 500);
					frame.setContentPane(supervisorPanel);	
				}
				else
				{
				int numberOfass = Integer.parseInt((answer.get(1)).toString()); // get the number of systems in the list
				this.comboBox.addItem("--Choose an Execution Leader--");
				for (int i=0; i < numberOfass ; i++ )
				{
					this.comboBox.addItem((String)answer.get(i+2));  // add systems to the list
				}
				}
				
				
			}
			else
				if(answer.get(0).equals("requestUser"))
				{

					(new Dialog("Name:"+answer.get(1))).setVisible(true);
					
				}
				else
					if(answer.get(0).equals("AbleToPair"))
					{
						(new Dialog("Paired Seccesfully")).setVisible(true);
						frame.setContentPane(new AssessorPairingPanel(frame, supervisorPanel));
						frame.setSize(460, 420);
					
						ArrayList<Object> requests= new ArrayList<Object>();
						requests.add("getReq");
						requests.add("7");
						AcademicFrame.myclient.sendMessageToServer(requests);
					
					}
		
	}

}
